package com.lanji.sensitive.entity.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @author Twilight
 * @desc
 * @createTime 2020-05-23-21:19
 */
@ApiModel(value = "分页对象")
public class PageDto {
    @ApiModelProperty(value = "当前页")
    public Integer pageIndex;
    @ApiModelProperty(value = "每页数量")
    public Integer size;
    @ApiModelProperty(value = "总数量")
    private Integer totalNum;
    @ApiModelProperty(value = "总页数")
    private Integer totalPage;
    @ApiModelProperty(value = "偏移量")
    private Integer offset;

    public PageDto() {
    }

    public PageDto(Integer pageIndex, Integer size) {
        if (pageIndex == null || pageIndex < 0) {
            pageIndex = 1;
        }
        if (size == null || size < 0) {
            size = 10;
        }
        this.pageIndex = pageIndex;
        this.size = size;
        this.offset = (pageIndex - 1) * size;
    }

    public PageDto(Integer pageIndex, Integer size, Integer totalNum) {
        if (pageIndex == null || pageIndex < 0) {
            pageIndex = 1;
        }
        if (size == null || size < 0) {
            size = 10;
        }
        this.pageIndex = pageIndex;
        this.size = size;
        this.totalNum = totalNum;
        this.offset = (pageIndex - 1) * size;
        this.totalPage = (totalNum / size) + 1;
    }

    public static PageDto get(Integer page, Integer size) {
        return new PageDto(page, size);
    }

    @Override
    public String toString() {
        return "Page{" +
                "pageIndex=" + pageIndex +
                ", size=" + size +
                ", totalNum=" + totalNum +
                ", totalPage=" + totalPage +
                '}';
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }

    public Integer getTotalNum() {
        return totalNum;
    }

    public void setTotalNum(Integer totalNum) {
        if (totalNum == null || totalNum < 0) {
            totalNum = 0;
        }
        this.totalNum = totalNum;
        this.totalPage = (totalNum / size) + 1;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public Integer getOffset() {
        return offset;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }
}
